package services

import (
	"context"
	"encoding/json"
	"fmt"
	"youku/youku-srv/basic/config"
	__ "youku/youku-srv/basic/proto"
	"youku/youku-srv/modle"
)

// server is used to implement helloworld.GreeterServer.
type Server struct {
	__.UnimplementedYouKuServer
}

// SayHello implements helloworld.GreeterServer
func (s *Server) Login(_ context.Context, in *__.LoginReq) (*__.LoginResp, error) {
	var user = modle.User{
		Phone:    in.Phone,
		Password: in.Password,
	}
	config.DB.Debug().Where("phone = ?", in.Phone).Find(&user)
	code, _ := config.Rdb.Get(config.Ctx, in.Phone).Result()
	if in.Code != code {
		return nil, fmt.Errorf("验证码错误不正确")
	}
	if user.ID == 0 {
		config.DB.Debug().Create(&user)
	}
	return &__.LoginResp{
		UserId: int64(user.ID),
	}, nil
}
func (s *Server) VideoAdd(_ context.Context, in *__.VideoAddReq) (*__.VideoAddResp, error) {
	var video = modle.Video{
		Title:        in.Title,
		Desc:         in.Desc,
		CoverUrl:     in.CoverUrl,
		Status:       int(in.Status),
		Types:        int(in.Types),
		Channel:      int(in.Channel),
		PlayCount:    int(in.PlayCount),
		LikeCount:    int(in.LikeCount),
		CommentCount: int(in.CommentCount),
		CollectCount: int(in.CollectCount),
	}
	config.DB.Debug().Create(&video)
	return &__.VideoAddResp{
		VideoId: int64(video.ID),
	}, nil
}
func (s *Server) VideoList(_ context.Context, in *__.VideoListReq) (*__.VideoListResp, error) {
	var video []modle.Video
	offset := (in.Page - 1) * in.Size
	result, err := config.Rdb.Get(config.Ctx, "视频列表").Result()
	if err != nil {
		return nil, err
	}
	if result != "" {
		json.Unmarshal([]byte(result), &video)
	} else {
		config.DB.Debug().Offset(int(offset)).Where("types = ?", in.Types).Where("channel = ?", in.Channel).Limit(int(in.Size)).Find(&video)
		jsonData, err := json.Marshal(&video)
		if err != nil {
			return nil, err
		}
		config.Rdb.Set(config.Ctx, "视频列表", jsonData, 0)
	}

	var b []*__.VideoListItem
	for _, c := range video {
		list := __.VideoListItem{
			Title:        c.Title,
			Desc:         c.Desc,
			CoverUrl:     c.CoverUrl,
			Status:       int64(c.Status),
			Types:        int64(c.Types),
			Channel:      int64(c.Channel),
			PlayCount:    int64(c.PlayCount),
			LikeCount:    int64(c.LikeCount),
			CommentCount: int64(c.CommentCount),
			CollectCount: int64(c.CollectCount),
		}
		b = append(b, &list)
	}
	return &__.VideoListResp{
		List: b,
	}, nil
}
func (s *Server) VideoData(_ context.Context, in *__.VideoDataReq) (*__.VideoDataResp, error) {
	var videoData modle.VideoData
	config.DB.Debug().Where("video_id = ?", in.VideoId).Find(&videoData)
	if videoData.ID == 0 {
		return nil, fmt.Errorf("这个视频不存在")
	}
	return &__.VideoDataResp{
		VideoId:      int64(videoData.VideoId),
		Title:        videoData.Title,
		Rating:       videoData.Rating,
		Introduction: videoData.Introduction,
	}, nil
}
func (s *Server) VideoEp(_ context.Context, in *__.VideoEpReq) (*__.VideoEpResp, error) {
	var videoEp []modle.VideoEp
	config.DB.Debug().Where("video_id = ?", in.VideoId).Find(&videoEp)

}
